#include<iostream>
using namespace std;
const int N = 1010;
int mem[N][N];
int im[N];
int w[N];
int n, m;
int dfs(int x, int spm)
{
    if (mem[x][spm])return mem[x][spm];
    int sum = 0;
    if (x > n)
    {
        return 0;
    }

    if (spm < w[x])
    {
        sum = dfs(x + 1, spm);
    }
    else
    {
        sum = max(dfs(x + 1, spm), dfs(x + 1, spm - w[x]) + (w[x] * im[x]));
    }
    return sum;
}
int main()
{
    cin >> m >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> w[i] >> im[i];
    }
    int ans = dfs(1, m);
    cout << ans;
    return 0;
}